package com.xhwl.data.mq.producer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;

/**
 * @Author：linruipeng
 * @Description：账号密码修改生产者
 * @Date：Created at 10:43 2023-03-20
 */
@Component
public class AccountPwdProducer {

    private static String UPDATE_ACCOUNT_PWD_TOPIC = "update_account_pwd";

    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;

    private final Logger log = LoggerFactory.getLogger(AccountProducer.class);

    public void updateAccountPwdTopic(String jsonStr) {
        ListenableFuture<SendResult<String, Object>> future = kafkaTemplate.send(UPDATE_ACCOUNT_PWD_TOPIC, jsonStr);
        future.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
            @Override
            //推送成功
            public void onSuccess(SendResult<String, Object> result) {
                log.info("主题 = {}， 账号密码修改数据推送成功 = {}", UPDATE_ACCOUNT_PWD_TOPIC, jsonStr);
            }
            @Override
            //推送失败
            public void onFailure(Throwable ex) {
                log.info("主题 = {}， 账号密码修改数据推送失败 = {}", UPDATE_ACCOUNT_PWD_TOPIC, jsonStr);
            }
        });
    }

}
